#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// NeatlyFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion

using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;

namespace Neatly.UI
{
    public class NRawImage : NMaskableGraphic
    {
        [SerializeField]
        Texture m_Texture;
        [SerializeField]
        Rect m_UVRect = new Rect(0f, 0f, 1f, 1f);

        protected NRawImage()
        {
            useLegacyMeshGeneration = false;
        }

        public override Texture mainTexture
        {
            get
            {
                if (m_Texture != null) return m_Texture;
                if (material != null && material.mainTexture != null)
                {
                    return material.mainTexture;
                }
                return s_WhiteTexture;
            }
        }

        public Texture texture
        {
            get
            {
                return m_Texture;
            }
            set
            {
                if (m_Texture == value)
                    return;
                m_Texture = value;
                SetVerticesDirty();
                SetMaterialDirty();
            }
        }

        public void SetTexture(Texture value)
        {
            if (m_Texture == value)
                return;
            ReleaseTexture();
            m_Texture = value;
            SetVerticesDirty();
            SetMaterialDirty();
        }

        protected override void OnDestroy()
        {
            ReleaseTexture();
        }

        public void ReleaseTexture()
        {
            if (m_Texture == null) return;
            var rt = m_Texture as RenderTexture;
            if (rt)
            {
                RenderTexture.ReleaseTemporary(rt);
                // Object.Destroy(rt);
            }
        }

        public Rect uvRect
        {
            get
            {
                return m_UVRect;
            }
            set
            {
                if (m_UVRect == value) return;
                m_UVRect = value;
                SetVerticesDirty();
            }
        }

        public override void SetNativeSize()
        {
            Texture tex = mainTexture;
            if (tex == null) return;
            int w = Mathf.RoundToInt(tex.width * uvRect.width);
            int h = Mathf.RoundToInt(tex.height * uvRect.height);
            rectTransform.anchorMax = rectTransform.anchorMin;
            rectTransform.sizeDelta = new Vector2(w, h);
        }

        protected override void OnPopulateMesh(VertexHelper vh)
        {
            Texture tex = mainTexture;
            vh.Clear();
            if (tex != null)
            {
                var r = GetPixelAdjustedRect();
                var v = new Vector4(r.x, r.y, r.x + r.width, r.y + r.height);
                var scaleX = tex.width * tex.texelSize.x;
                var scaleY = tex.height * tex.texelSize.y;
                {
                    var color32 = color;
                    vh.AddVert(new Vector3(v.x, v.y), color32, new Vector2(m_UVRect.xMin * scaleX, m_UVRect.yMin * scaleY));
                    vh.AddVert(new Vector3(v.x, v.w), color32, new Vector2(m_UVRect.xMin * scaleX, m_UVRect.yMax * scaleY));
                    vh.AddVert(new Vector3(v.z, v.w), color32, new Vector2(m_UVRect.xMax * scaleX, m_UVRect.yMax * scaleY));
                    vh.AddVert(new Vector3(v.z, v.y), color32, new Vector2(m_UVRect.xMax * scaleX, m_UVRect.yMin * scaleY));

                    vh.AddTriangle(0, 1, 2);
                    vh.AddTriangle(2, 3, 0);
                }
            }
        }
    }
}
